package walkera.cn.http;
import android.content.Context;

import java.util.ArrayList;

import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import walkera.cn.bean.SampReponse;

public class XyApi {
    private static XyApi XyApi;
    private XyApiServe XyApiServe;

    private XyApi(Context context) {
        XyApiServe = HttpClient.getInstance( HostConfig.getBaseUrl(), context).createApi(XyApiServe.class);
    }

    public static XyApi getInstance(Context context) {
        if (XyApi == null) {
            XyApi = new XyApi(context);
        }
        return XyApi;
    }


    public void  openCloseLed( SingleObserver<Void> singleObserver , boolean isOpen , int ledColor ) {
        XyApiServe.openCloseLed( isOpen , ledColor )
                /*.flatMap(new Function<ArrayList<String>, SingleSource<ArrayList<String> >>() {
                    @Override
                    public SingleSource<ArrayList<String>> apply(ArrayList<String>  list) throws Exception {
                        // 数据过滤
                        return Single.just(list);
                    }
                })*/
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(singleObserver)  ;
    }


    public void  getSystemInfo( SingleObserver<SampReponse> singleObserver   ) {
        XyApiServe.getSystemInfo(  )
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(singleObserver)  ;
    }

    public void  ledFlicker( SingleObserver<Void> singleObserver ,int[] colors ,  int sleepTime  ) {
        XyApiServe.ledFlicker( colors, sleepTime )
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(singleObserver)  ;
    }


    public void  ledBreath( SingleObserver<Void> singleObserver ,int  ledColor , int sleepTime1 ,  int sleepTime2  ) {
        XyApiServe.ledBreath( ledColor, sleepTime1 ,sleepTime2 )
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(singleObserver)  ;
    }


}
